package com.eastedu.template.util;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.plugin.toc.TOCRenderPolicy;
import com.deepoove.poi.policy.DocumentRenderPolicy;
import com.deepoove.poi.policy.ListRenderPolicy;
import com.deepoove.poi.policy.ParagraphRenderPolicy;

import java.io.InputStream;
import java.util.Objects;

/**
 * The interface Render config util.
 *
 * @author superman
 */
public interface RenderConfigUtil {
    /**
     * Create template xwpf template.
     *
     * @param template  the template
     * @param configure 自定义配置
     * @return the xwpf template
     */
    static XWPFTemplate createTemplate(InputStream template, Configure configure) {
        return XWPFTemplate.compile(template, determinedConfigure(configure));
    }

    /**
     * Determined configure.
     *
     * @param configure 自定义配置
     * @return 默认配置 configure
     */
    static Configure determinedConfigure(Configure configure) {
        if (Objects.isNull(configure)) {
            configure = Configure.builder()
                    .addPlugin('_', new DocumentRenderPolicy())
                    .addPlugin(':', new ListRenderPolicy())
                    .addPlugin('~', new LoopRowTableRenderPolicy(true))
                    .addPlugin('&', new TOCRenderPolicy())
                    ///                    .addPlugin('&', new TocRenderFalsePolicy())
                    .addPlugin('=', new ParagraphRenderPolicy())
                    .useSpringEL(false)
                    .build();
        }
        return configure;
    }
}
